package drds.data_propagate.binlog_event.binlog_event.load_infile_replication;

import drds.data_propagate.binlog_event.BinLogEvent;
import drds.data_propagate.binlog_event.Buffer;
import drds.data_propagate.binlog_event.binlog_event.Header;
import drds.data_propagate.binlog_event.binlog_event.binlog_management.FormatDescriptionEvent;
import lombok.Getter;
import lombok.Setter;

public class AppendBlockEvent extends BinLogEvent {

    /* AB = "Append Block" */
    public static final int append_block_event_offset = 0;
    @Setter
    @Getter
    private final Buffer buffer;
    @Setter
    @Getter
    private final int blockLength;
    @Setter
    @Getter
    private final long fileId;

    public AppendBlockEvent(Header header, Buffer buffer, FormatDescriptionEvent formatDescriptionEvent) {
        super(header);

        final int commonHeaderLength = formatDescriptionEvent.commonHeaderLength;
        final int eventPostHeaderLength = formatDescriptionEvent.eventPostHeaderLength[header.eventType - 1];
        final int totalHeaderLength = commonHeaderLength + eventPostHeaderLength;

        buffer.newReadedIndex(commonHeaderLength + append_block_event_offset);
        fileId = buffer.getNextLittleEndian32UnsignedLong();

        buffer.newReadedIndex(eventPostHeaderLength);
        blockLength = buffer.limit() - totalHeaderLength;
        this.buffer = buffer.duplicate(blockLength);
    }

    public final byte[] getData() {
        return buffer.bytesCopy();
    }
}
